package com.bootx.service.impl;

import com.bootx.entity.BetRecord;
import com.bootx.entity.LotteryRecord;
import com.bootx.pojo.PredictInfo;
import com.bootx.service.BetRecordService;
import com.bootx.util.JsonUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Strings;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author black
 */
@Service
public class BetRecordServiceImpl extends BaseServiceImpl<BetRecord,Long> implements BetRecordService {

    @Override
    public BetRecord findBySn(Long sn){
        try {
            Long l = jdbcTemplate.queryForObject("select id from betrecord where sn=?", Long.class, sn%100000);
            return super.find(l);
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public boolean isBet(Long sn) {
        return findBySn(sn) != null;
    }

    @Override
    public Integer getFailCount(LotteryRecord lotteryRecord) {
        return Integer.valueOf(redisService.get("failCount1"));
    }


    @Override
    public void load(LotteryRecord lotteryRecord) {
        BetRecord betRecord = findBySn(lotteryRecord.getSn());
        /*if(betRecord==null){
            betRecord =  new BetRecord();
            String s = redisService.get("sendInfo");
            Map<String,Object> sendInfo = JsonUtils.toObject(s, new TypeReference<Map<String, Object>>() {
            });
            // {"msg":"上一局13669331未投，本次投注100","money":"100","stop":1,"num":1,"sn":69336,"type":"单","send":0}
            betRecord.setMoney(Double.parseDouble(sendInfo.get("money") + ""));
            betRecord.setNum(Integer.parseInt(sendInfo.get("num") + ""));
            betRecord.setSn(Long.parseLong(sendInfo.get("sn") + ""));
            betRecord.setBetType(sendInfo.get("type") + "");
            betRecord.setLotteryCode(lotteryRecord.getLotteryCode());
            betRecord = super.save(betRecord);
        }*/
        if(betRecord!=null){
            Integer num = betRecord.getNum();
            String betType = betRecord.getBetType();
            List<Integer> drawNums = lotteryRecord.getDrawNums();
            if(drawNums==null || drawNums.isEmpty()){
                return;
            }
            Integer i = drawNums.get(num - 1);
            List<PredictInfo> predictInfos = lotteryRecord.getPredictInfo();
            if(predictInfos==null || predictInfos.isEmpty()){
                return;
            }
            PredictInfo predictInfo = predictInfos.get(num - 1);
            if (Strings.CI.equalsAny(betType,"单") || Strings.CI.equalsAny(betType,"双")){
                // 单双
                Integer predictOdd = predictInfo.getPredictOdd();
                if(predictOdd==1 && Strings.CI.equalsAny(betType,"单")){
                    betRecord.setWin(1);
                    redisService.set("failCount1","0");
                }else if(predictOdd==0 && Strings.CI.equalsAny(betType,"双")){
                    betRecord.setWin(1);
                    redisService.set("failCount1","0");
                }else {
                    betRecord.setWin(0);
                    redisService.increment("failCount1");
                }
            }else if(Strings.CI.equalsAny(betType,"大") || Strings.CI.equalsAny(betType,"小")){
                // 大小
                Integer predictLarge = predictInfo.getPredictLarge();
                if(predictLarge==1 && Strings.CI.equalsAny(betType,"大")){
                    betRecord.setWin(1);
                    redisService.set("failCount1","0");
                }else if(predictLarge==0 && Strings.CI.equalsAny(betType,"小")){
                    betRecord.setWin(1);
                    redisService.set("failCount1","0");
                }else {
                    betRecord.setWin(0);
                    redisService.increment("failCount1");
                }
            }else if(Strings.CI.equalsAny(betType,"数字")){
                // 数字
                String predictNums = predictInfo.getPredictNums();
                if(predictNums.indexOf(i)>=0){
                    betRecord.setWin(1);
                }else {
                    betRecord.setWin(0);
                }
            }
            if(betRecord.getWin()==0){
                // 说明输了
                redisService.set("failType","1");
            }else{
                redisService.delete("failType");
            }
            super.update(betRecord);
        }else{
            redisService.set("failCount","0");
        }
    }
}
